package com.atguigu.gulimall.auth.web;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberResponVo;
import com.atguigu.gulimall.auth.feign.MemberFeignService;
import com.atguigu.gulimall.auth.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ming
 * @create 2022/5/6
 * @description:
 */
@Controller
@Slf4j
public class ApiWeiBoController {
    @Autowired
    private MemberFeignService memberFeignService;
    /**
     * 请求成功 微博会返回来一个code码
     * @return
     */
    @GetMapping("/oauth2.0/weibo/success")
    public String weibo(@RequestParam("code") String code, HttpSession session) throws Exception {
        //根据code换取access_token
        //登录成功跳到首页    client_id 2077705774
        Map<String, String> map = new HashMap<>();
        map.put("client_id","2077705774");
        map.put("client_secret","40af02bd1c7e435ba6a6e9cd3bf799fd");
        map.put("grant_type","authorization_code");
        map.put("redirect_uri","http://auth.gulimall.com/oauth2.0/weibo/success");
        map.put("code",code);
        HttpResponse response = HttpUtils.doPost("https://api.weibo.com",
                "/oauth2/access_token",
                "post",
                new HashMap<String, String>(),
                map,
                new HashMap<String, String>()
        );
        int statusCode = response.getStatusLine().getStatusCode();
        if(statusCode==200){
            HttpEntity entity = response.getEntity();
            String json = EntityUtils.toString(entity);
            SocialUser socialUser = JSONObject.parseObject(json, SocialUser.class);
            //如果当前用户是第一次进网站,自动注册进来为当前社交用户生成一个会员信息
            R r = memberFeignService.oauth2login(socialUser);
            if(r.getCode()==0){
                MemberResponVo data = r.getData("data", new TypeReference<MemberResponVo>() {});
                //第一次使用session,命令浏览器保存卡号,jsessionid这个cookie
                //gulimall.com auth.gulimall.com order.gulimall.com
                //发卡的时候即使子域系统发的卡也能让父域直接使用
                session.setAttribute(AuthServerConstant.LOGIN_USER,data);
                log.info("用户信息: {}",data);
                //登录成功跳回首页
                return "redirect:http://gulimall.com";
            }else{
                return "redirect:http://auth.gulimall.com/login.html";
            }
        }else{
            return "redirect:http://auth.gulimall.com/login.html";
        }

    }
}
